package com.gsafety.wellsfort.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

/**
 * @author tangjiawei
 * @version 1.0
 * @date 2019/5/21 12:33
 */
@Slf4j
@Component
public class CacheManager<T> {
    @Autowired
    private StringRedisTemplate redisTemplate;


    public void remove(String key) {
        redisTemplate.delete(key);
    }

    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public void set(String key, String value, long second) {
        redisTemplate.opsForValue().set(key, value, second, TimeUnit.SECONDS);
    }

    public void set(String key, T value) {
        String v = JSON.toJSONString(value);
        set(key, v);
    }

    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public T get(String key, Class<T> cls) {
        String v = redisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(v)) {
            return null;
        }
        return JSONObject.parseObject(v, cls);
    }

    /**
     * 值自加1
     *
     * @param key
     */
    public void incr(String key) {
        redisTemplate.opsForValue().increment(key);
    }


    /**
     * hash
     *
     * @param hashKey
     * @param key
     */
    public void setHashKey(String hashKey, String key, String value) {
        Map<String, String> map = null;
        String hashValue = redisTemplate.opsForValue().get(hashKey);
        if (StringUtils.isEmpty(hashValue)) {
            map = new TreeMap<>();
        } else {
            map = (Map) JSONObject.parseObject(hashValue);
        }
        map.put(key, value);
        String v = JSON.toJSONString(map);
        log.info("key={},value={}", hashKey, v);
        redisTemplate.opsForValue().set(hashKey, v);
    }

    /**
     * hash
     *
     * @param hashKey
     * @param key
     */
    public void addHashKeyIncr(String hashKey, String key) {
        Map<String, Integer> map = null;
        String value = redisTemplate.opsForValue().get(hashKey);
        if (StringUtils.isEmpty(value)) {
            map = new TreeMap<>();
        } else {
            map = (Map) JSONObject.parseObject(value);
        }
        if (map.containsKey(key)) {
            map.put(key, map.get(key) + 1);
        } else {
            map.put(key, 1);
        }
        String v = JSON.toJSONString(map);
        log.info("key={},value={}", hashKey, v);
        redisTemplate.opsForValue().set(hashKey, v);
    }

    /**
     * hash
     *
     * @param hashKey
     * @param key
     */
    public void delHashKey(String hashKey, String key) {
        Map<String, Integer> map = null;
        String value = redisTemplate.opsForValue().get(hashKey);
        if (StringUtils.isNoneEmpty(value)) {
            map = (Map) JSONObject.parseObject(value);
            map.remove(key);
            String v = JSON.toJSONString(map);
            log.info("key={},value={}", hashKey, v);
            redisTemplate.opsForValue().set(hashKey, v);
        }
    }

    public String getHashValue(String hashKey, String key) {
        String value = redisTemplate.opsForValue().get(hashKey);
        if (StringUtils.isNotEmpty(value)) {
            Map<String, String> map = (Map) JSONObject.parseObject(value);
            return map.get(key);
        }
        return null;
    }

    /**
     * set
     *
     * @param setKey
     * @param key
     */
    public void addListKey(String setKey, String key) {
        List<String> list = null;
        String value = redisTemplate.opsForValue().get(setKey);
        if (StringUtils.isEmpty(value)) {
            list = new ArrayList<>();
        } else {
            list = (List) JSONArray.parseArray(value);
        }
        if (!list.contains(key)) {
            list.add(key);
        }
        String v = JSON.toJSONString(list);
        log.info("key={},value={}", setKey, v);
        redisTemplate.opsForValue().set(setKey, v);
    }

    /**
     * 获取hash
     *
     * @param hashKey
     * @return
     */
    public Map getHash(String hashKey) {
        String value = redisTemplate.opsForValue().get(hashKey);
        if (StringUtils.isNotEmpty(value)) {
            Map map = (Map) JSONObject.parseObject(value);
            return map;
        }
        return null;
    }

    /**
     * set
     *
     * @param setKey
     */
    public List<String> getList(String setKey) {
        String value = redisTemplate.opsForValue().get(setKey);
        if (StringUtils.isNotEmpty(value)) {
            List<String> list = (List) JSONArray.parseArray(value);
            return list;
        }
        return null;
    }
}
